package com.library.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.dto.PageQueryDTO;
import com.library.mapper.BookMapper;
import com.library.mapper.CategoryMapper;
import com.library.result.PageResult;
import com.library.service.StatisticsService;
import com.library.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<BookCountByCategoryVO> getBookCountByCategory() {
        List<BookCountByCategoryVO> bookCountByCategoryVOList = bookMapper.getBookCountByCategory();
        return bookCountByCategoryVOList;
    }

    @Override
    public List<BookCountByYearVO> getBookCountByPublishedYear() {
        List<BookCountByYearVO> bookCountByYearVOList = bookMapper.getBookCountByPublishedYear();
        return bookCountByYearVOList;
    }

    @Override
    public List<BookCountByPriceVO> getBookCountByPrice() {
        List<BookCountByPriceVO> bookCountByPriceVOList = bookMapper.getBookCountByPrice();
        return bookCountByPriceVOList;
    }

    @Override
    public PageResult getBookCountByBorrowed(PageQueryDTO pageQueryDTO) {
        if (pageQueryDTO.getPage() == null) {
            pageQueryDTO.setPage(1);
        }
        if (pageQueryDTO.getPageSize() == null) {
            pageQueryDTO.setPageSize(10);
        }
        Integer limit = null;
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<BookCountByBorrowedVO> page = bookMapper.getBookCountByBorrowed(limit);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult getBookCountByBrowse(PageQueryDTO pageQueryDTO) {
        if (pageQueryDTO.getPage() == null) {
            pageQueryDTO.setPage(1);
        }
        if (pageQueryDTO.getPageSize() == null) {
            pageQueryDTO.setPageSize(10);
        }
        Integer limit = null;
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<BookCountByBrowseVO> page = bookMapper.getBookCountByBrowse(limit);
        return new PageResult(page.getTotal(), page.getResult());

    }


}
